<template>
    <div>
        <!-- 面包屑 -->
        <el-breadcrumb separator="/">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>商品管理</el-breadcrumb-item>
            <el-breadcrumb-item>商品分类</el-breadcrumb-item>
        </el-breadcrumb>
        <!-- 搜索 -->
        <div style="margin-top: 15px;">
            <el-button type="success" plain @click='addDialogCat'>添加分类</el-button>
        </div>
        <tree-grid :treeStructure="true" :columns="columns" :data-source="dataSource" @deleteCate="deleteCategory" @editCate="editCategory">
        </tree-grid>
        <!-- 点击添加分类弹出层 -->
        <el-dialog title="添加分类" :visible.sync="addDialogFormVisible">
            <el-form :model="aadForm" label-width='500'>
                <el-form-item label="分类名称">
                    <el-input v-model="aadForm.name" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="父级名称">
                    <el-cascader expand-trigger="hover" :props="addDefaultProps" :options="addOptionsList" v-model="addSelectedOptions" @change="handleChange">
                    </el-cascader>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="addDialogFormVisible = false">取 消</el-button>
                <el-button type="primary" @click="addDialogFormVisible = false">确 定</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import TreeGrid from '@/components/TreeGrid/TreeGrid'
import { getAllcategories } from '@/api/index.js'
export default {
  data () {
    return {
      aadForm: {
        name: ''
      },
      addOptionsList: [],
      addSelectedOptions: [],
      addDefaultProps: {
        value: 'cat_id',
        label: 'cat_name',
        children: 'children'
      },

      addDialogFormVisible: false,
      dataSource: [],
      columns: [
        {
          text: '分类名称',
          dataIndex: 'cat_name',
          width: ''
        },
        {
          text: '是否有效',
          dataIndex: 'cat_deleted',
          width: ''
        },
        {
          text: '排序',
          dataIndex: 'cat_level',
          width: ''
        }
      ]
    }
  },
  components: {
    'tree-grid': TreeGrid
  },
  methods: {
    //   点击添加分类
    addDialogCat () {
      this.addDialogFormVisible = true
      getAllcategories(3).then(res => {
        console.log(res)
        this.addOptionsList = res.data
      })
    },

    handleChange (value) {
      console.log(value)
    },
    getAllcategoriesData () {
      getAllcategories(3).then(res => {
        console.log(res)
        this.dataSource = res.data
      })
    },
    deleteCategory (cid) {
      console.log(cid)
    },
    editCategory (cid) {
      console.log(cid)
    }
  },
  mounted () {
    this.getAllcategoriesData()
  }
}
</script>

<style lang="scss" scoped>
/* 覆盖element导航菜单的样式 */
.el-breadcrumb {
  background-color: #f5f5f5;
  height: 45px;
  font-size: 15px;
  padding-left: 10px;
  line-height: 45px;
  margin-bottom: 15px;
}
</style>
